package mc.support.crawler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA. <br/>
 * User: francis    <br/>
 * Date: 13-11-6    <br/>
 * Time: 17:44  <br/>
 */
public class TaobaoSearchQueryFactory {

    public static final String COMPREHENSIVE = "default";
    public static final String POPULARITY = "renqi-desc";
    public static final String SALES = "sale-desc";
    public static final String PRICE_ASC = "price-asc";
    public static final String PRICE_DESC = "price-desc";

    public static final Map<SortType, String> sortTypeName = new HashMap<SortType, String>() {
        {
            put( SortType.comprehensive, COMPREHENSIVE );
            put( SortType.popularity, POPULARITY );
            put( SortType.sales, SALES );
            put( SortType.priceAsc, PRICE_ASC );
            put( SortType.priceDesc, PRICE_DESC );
        }
    };

    private final static DateFormat format = new SimpleDateFormat("yyyyMMdd");

    private final static String TB_SEARCH_HOST = "s.taobao.com";
    private final static String TB_SEARCH_PATH = "/search";

    private final static Map<String, String> TB_STATIC_PARAMS = new HashMap<String, String>() {
        {
//            put( "stats_click", "search_radio_all" );
            put( "tab", "all" );
//            put( "commend", "all" );
//            put( "ssid", "s5-e" );
//            put( "style", "grid" );
//            put( "newpre", "null" );

            put( "promote", "0");
        }
    };

    public static QueryWrapper singleQuery(String keyword, SortType sortType, Integer pageIndex) {
        Map<String, String> queryMap = new HashMap<String, String>();

        queryMap.put( "q", keyword );
        queryMap.put( "sort", sortTypeName.get( sortType ) );
        if ( null != pageIndex && pageIndex > 0 ) {
            queryMap.put( "s", String.valueOf( pageIndex * 40 ) );
        }
        queryMap.put( "initiative_id", "tbindexz_" + format.format( new Date() ));

        queryMap.putAll( TB_STATIC_PARAMS );

        return new QueryWrapper( new Query( Query.HTTP_PREFIX,
                TB_SEARCH_HOST, TB_SEARCH_PATH, Query.GB2312, queryMap ), pageIndex ) ;
    }

    public static QueryWrapper singleQuery(String keyword, String filter, SortType sortType, Integer pageIndex) {
        Map<String, String> queryMap = new HashMap<String, String>();

        queryMap.put( "q", keyword );
        queryMap.put( "sort", sortTypeName.get( sortType ) );
        if( null != filter){
            queryMap.put( "filter", filter );
        }
        if ( null != pageIndex && pageIndex > 0 ) {
            queryMap.put( "s", String.valueOf( pageIndex * 40 ) );
        }
        queryMap.put( "initiative_id", "tbindexz_" + format.format( new Date() ));

        queryMap.putAll( TB_STATIC_PARAMS );

        return new QueryWrapper( new Query( Query.HTTP_PREFIX,
                TB_SEARCH_HOST, TB_SEARCH_PATH, Query.GB2312, queryMap ), pageIndex ) ;
    }

    public static List<QueryWrapper> createQueries(String keyword, SortType sortType,
                                                   Integer begin, Integer offset) {
//        List<QueryWrapper> queries = null;
//
//        if ( beginDecade <= endDecade ) {
//            int decade = 10;
//            int range = endDecade - beginDecade + 1;
//            queries = new ArrayList<QueryWrapper>( range );
//            for ( int diff = 0; diff < range; diff++ ) {
//                int pageIndex = beginDecade + ( diff * decade );
//                queries.add( singleQuery( keyword, sortType, pageIndex ) );
//            }
//        }

        List<QueryWrapper> queries = new ArrayList<QueryWrapper>(offset);
        int len = begin + offset;
        for ( int i = begin; i < len; i++ ) {
            queries.add( singleQuery( keyword, sortType, i ) );
        }

        return queries;
    }

}
